﻿using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;

namespace ZH.GPT.Extend
{
    /// <summary>
    /// HttpContext扩展
    /// </summary>
    public static class HttpContextExtensions
    {
        /// <summary>
        /// 获取用户id
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static Guid? UserId(this HttpContext context)
        {

            var userId = context.Request.Headers["UserId"];
            if (!string.IsNullOrEmpty(userId))
            {
                if (Guid.TryParse(userId.ToString(), out var uid))
                {
                    return uid;
                }
            }
            return null;

        }

        /// <summary>
        /// 获取租户id
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static Guid? TenantId(this HttpContext context)
        {
            //tenant_id
            var tenantId = context.Request.Headers["TenantId"];
            if (!string.IsNullOrEmpty(tenantId))
            {
                if (Guid.TryParse(tenantId.ToString(), out var uid))
                {
                    return uid;
                }
            }
            return null;
        }

        /// <summary>
        /// 获取租户名称，如果是平台返回 平台
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static string TenantName(this HttpContext context)
        {
            var tenantName = context.Request.Headers["TenantName"];
            if (!string.IsNullOrEmpty(tenantName))
            {
               return System.Web.HttpUtility.UrlDecode(tenantName) ;
            }
            return null;
        }

        /// <summary>
        /// 获取公司类型， 如果是平台会返回 -1， 
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static int? TenantType(this HttpContext context)
        {
            var tenantType = context.Request.Headers["TenantType"];
            if (!string.IsNullOrEmpty(tenantType))
            {
                return Convert.ToInt32(tenantType);
            }
            return null;
        }

        /// <summary>
        /// 获取账号
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static string Account(this HttpContext context)
        {
            var account = context.Request.Headers["Account"];

            if (!string.IsNullOrEmpty(account))
            {
                return account.ToString();
            }
            return "";
        }

        /// <summary>
        /// 获取用户名称
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static string UserName(this HttpContext context)
        {
            var userName = context.Request.Headers["UserName"];

            if (!string.IsNullOrEmpty(userName))
            {
                return System.Web.HttpUtility.UrlDecode(userName);
            }
            return "";
        }

        /// <summary>
        /// 获取平台类型
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static int?  PlatformType(this HttpContext context)
        {
            var platformType = context.Request.Headers["PlatformType"];
            if (!string.IsNullOrEmpty(platformType))
            {
                if (int.TryParse(platformType, out var pid))
                {
                    return pid;
                }
            }
            return null;
        }
    }
}
